package com.code.origin.utils.other;

import cn.hutool.core.util.NumberUtil;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * 常用数字工具类
 * @author my
 **/
public class NumberUtils extends NumberUtil {

	/**
	 * 数字是否不为空，且大于0
	 * @param number
	 * @return true：是,false：否
	 */
	public static boolean isGt0IntegerString(String integerString) {
		if (!isNumber(integerString.trim())) {
			return false;
		}
		return isGt0(Integer.valueOf(integerString.trim()));
	}
	
	/**
	 * 数字是否不为空，且大于等于0
	 * @param number
	 * @return true：是,false：否
	 */
	public static boolean isGte0IntegerString(String integerString) {
		if (!isNumber(integerString.trim())) {
			return false;
		}
		return isGte0(Integer.valueOf(integerString.trim()));
	}
	
	/**
	 * 数字是否不为空，且大于0
	 * @param number
	 * @return true：是,false：否
	 */
	public static boolean isGt0(Object number) {
		
		return isNotEmpty(number);
	}
	
	/**
	 * 数字是否为空，或小于等于0
	 * @param number
	 * @return true：是,false：否
	 */
	public static boolean isNotGt0(Object number) {
		
		return !isGt0(number);
	}
	
	/**
	 * 数字是否为空，或小于0
	 * @param number
	 * @return true：是,false：否
	 */
	public static boolean isNotGte0(Object number) {
		
		return !isGte0(number);
	}
	
	/**
	 * 数字是否不为空，且大于等于0
	 * @param number
	 * @return true：是,false：否
	 */
	public static boolean isGte0(Object number) {
		
		boolean res = false;
		if (number != null) {
			if (number instanceof Integer && (Integer)number >= 0) {
				res = true;
			} else if (number instanceof Double && (Double)number >= 0d) {
				res = true;
			} else if (number instanceof Float && (Float)number >= 0f) {
				res = true;
			} else if (number instanceof Long && (Long)number >= 0) {
				res = true;
			}
		}
		
		return res;
	}
	
	/**
	 * 数字是否不为空，且大于0
	 * @param number
	 * @return true：是,false：否
	 */
	public static boolean isNotEmpty(Object number) {
		boolean res = false;
		if (number != null) {
			if (number instanceof Integer && (Integer)number > 0) {
				res = true;
			} else if (number instanceof Double && (Double)number > 0d) {
				res = true;
			} else if (number instanceof Float && (Float)number > 0f) {
				res = true;
			} else if (number instanceof Long && (Long)number > 0) {
				res = true;
			}
		}
		
		return res;
	}
	
	/**
	 * 数字是否为空，或小于等于0
	 * @param number
	 * @return true：是,false：否
	 */
	public static boolean isEmpty(Object number) {
		boolean res = false;
		if (number == null) {
			return true;
		} else {
			if (number instanceof Integer && (Integer)number <= 0) {
				res = true;
			} else if (number instanceof Double && (Double)number <= 0d) {
				res = true;
			} else if (number instanceof Float && (Float)number <= 0f) {
				res = true;
			} else if (number instanceof Long && (Long)number <= 0) {
				res = true;
			}
		}
		
		return res;
	}
	
    /**
     * 得到double数值
     * 传入值为空或发生异常，返回0
     * @param value
     * @return
     */
    public static double doubleValueOf(Object value){
        double tmp = 0;
        if(value == null){
            return tmp;
        }
        try {
            tmp = Double.parseDouble(value.toString());
        } catch (NumberFormatException e) {
        }
        return tmp;
    }

    /**
     * 得到int数值
     * 传入值为空或发生异常，返回0
     * @param value
     * @return
     */
    public static int intValueOf(Object value){
        int tmp = 0;
        if(value == null){
            return tmp;
        }
        try {
            tmp = Integer.parseInt(value.toString());
        } catch (NumberFormatException e) {
        }
        return tmp;
    }

    /**
     * 数字格式化
     * @param value        需要格式化的值
     * @param maxFragDigit 最大小数位数
     * @param minFragDigit 最小小树位数
     * @return
     */
    public static String doubleFormat(double value,int maxFragDigit, int minFragDigit){
        return doubleFormat(value, maxFragDigit, minFragDigit, Integer.MAX_VALUE, 1);
    }

    /**
     * 数字格式化
     * @param value
     * @param maxFragDigit
     * @param minFragDigit
     * @param intMaxDigit
     * @param intMinDigit
     * @return
     */
    public static String doubleFormat(double value,int maxFragDigit, int minFragDigit,int intMaxDigit,int intMinDigit){
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(maxFragDigit);
        nf.setMinimumFractionDigits(minFragDigit);
        nf.setMaximumIntegerDigits(intMaxDigit);
        nf.setMinimumIntegerDigits(intMinDigit);
        return nf.format(value);
    }

    /**
     * 分转元
     */
    public static  double fen2Yuan(Integer money){
        if(money==null ){
            return 0;
        }
        return Double.valueOf(money)/100;
    }
    
    /**
     * 分转元
     */
    public static  int yuan2fen(Double money){
        if(money==null ){
            return 0;
        }
        return (int) (money*100);
    }
    
    /**
     * 分转元
     */
    public static Float fen2YuanFloat(Integer moneyFen){
        if(moneyFen == null) {
            return null;
        }
        return Float.valueOf(moneyFen)/100;
    }
    
    /**
     * 元转分
     */
    public static Integer yuan2FenFloat(Float moneyYuan){
        if(moneyYuan == null) {
            return null;
        }
        //原实现会丢失精度 NumberUtils.yuan2FenFloat(150.76f)，返回15075;
//        return (int) (moneyYuan*100);
        
        //以百分比方式计数
        DecimalFormat decimalFormat=new DecimalFormat("#%");
        
        //format 返回的是百分比字符串
		String p=decimalFormat.format(moneyYuan);
		
		//去掉百分比符号
		p = p.substring(0, p.length()-1);
		return Integer.valueOf(p);
    }
    

    public static String normalScoreFormat(double value){
        return doubleFormat(value, 1, 0, 3, 1);
    }

}
